package com.util.boxs.bodys;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;

import java.awt.*;

/**
 * 原型刚体
 */
public class CircleBody extends HitBody{
    protected int pixelRadius;
    //box2d中的半径长度
    protected float radius;//半径

    public CircleBody(int pixelX,int pixelY,int pixelRadius,boolean isStatic){
        FixtureDef fd = getDefaultFixtureDef();
        init(pixelX,pixelY,pixelRadius,fd,isStatic);
    }
    public CircleBody(int pixelX,int pixelY,int pixelRadius,float density,float friction,float restitution,boolean isStatic){
        FixtureDef fd = new FixtureDef();
        fd.density = 5f;
        fd.friction = 0.3f;
        fd.restitution = 0.2f;
        init(pixelX,pixelY,pixelRadius,fd,isStatic);
    }
    private void init(int pixelX,int pixelY,int pixelRadius,FixtureDef fd,boolean isStatic){
        this.pixelRadius = pixelRadius;
        this.radius = pixel2Mile(pixelRadius);

        fd.shape = getShape(this.radius);

        BodyDef bodyDef = new BodyDef();
        bodyDef.angularDamping = 0.5f;
        bodyDef.position= getPixel2MileVec2(pixelX+pixelRadius,pixelY-pixelRadius);
        bodyDef.type = isStatic?BodyType.STATIC:BodyType.DYNAMIC;

        body = create(bodyDef,this);
        body.createFixture(fd);

        body.setSleepingAllowed(true);

    }
    public CircleBody(int pixelX,int pixelY,int pixelRadius){
        this(pixelX,pixelY,pixelRadius,false);
    }

    @Override
    public void setPosition(int pixelX, int pixelY) {
        body.setTransform(getPixel2MileVec2(pixelX+pixelRadius,pixelY-pixelRadius),body.getAngle());
    }



    @Override
    public int getPixelX(){
        int pixelX = mile2Pixel(body.getPosition().x-radius);
        return pixelX;
    }

    @Override
    public int getPixelY(){
        int pixelY = toPixelHeight(body.getPosition().y+radius);
        return pixelY;
    }
    public int getWidthOrHeight(){
        return mile2Pixel(radius*2);
    }

    private CircleShape getShape(float radius){
        CircleShape shape = new CircleShape();
        shape.setRadius(radius);
        return shape;
    }
}
